<template>
  <MyDialog
    :title="`${id ? '编辑' : '新建'}团队`"
    show-cancel
    :visible="showDialog"
    :ok-loading="btnLoading"
    width="800px"
    @close="close"
    @ok="handleConfirmClick"
  >
    <el-form
      ref="baseForm"
      v-loading="loading"
      :model="modalFormData"
      label-suffix=":"
      label-width="100px"
      :rules="modalRules"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="modalFormData.name" placeholder="请输入团队名称" />
      </el-form-item>
      <!-- <el-form-item label="结算类型" prop="name">
        <el-select
          v-model="modalFormData.billType"
          style="width: 100%"
          placeholder="请选择结算类型"
          clearable
        >
          <el-option
            v-for="option in BILL_TYPE"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="备注" prop="name">
        <el-input v-model="modalFormData.remark" placeholder="请输入团队备注" />
      </el-form-item>
      <!-- 设置分红比例 -->
      <el-row
        v-for="(item, index) in modalFormData.profits"
        :key="index"
        type="flex"
        align="center"
      >
        <div style="flex: 1; overflow: hidden">
          <el-form-item
            label="等级"
            :prop="'profits.' + index + '.gradeLevel'"
            :rules="modalRules.profits.gradeLevel"
          >
            <el-select
              v-model="item.gradeLevel"
              style="width: 100%"
              placeholder="请选择等级"
              clearable
            >
              <el-option
                v-for="option in gradeList"
                :key="option.level"
                :label="option.name"
                :value="option.level"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-row
            v-if="modalFormData.profits[index].gradeLevel"
            type="flex"
            justify="space-between"
            class="input-number-row"
          >
            <el-form-item
              label="分销成本"
              :prop="'profits.' + index + '.recommendProfit'"
              :rules="modalRules.profits.recommendProfit"
              class="input-number"
            >
              <el-input-number
                v-model="item.recommendProfit"
                placeholder="请输入分销成本"
              />
            </el-form-item>
            <el-form-item
              label="流量分润比例"
              :prop="'profits.' + index + '.bandwidthProfit'"
              :rules="modalRules.profits.bandwidthProfit"
              class="input-number"
              style="position: relative; right: 46px"
            >
              <el-input-number v-model="item.bandwidthProfit" :min="0" :max="1"
                               :step="0.1"
                               placeholder="请输入流量分润比例"
              />
            </el-form-item>
          </el-row>
        </div>
        <i
          v-if="index === 0"
          class="icon el-icon-circle-plus-outline"
          @click="handleAdd"
        ></i>
        <i
          v-else
          class="icon el-icon-remove-outline"
          @click="handleRemove(index)"
        >
        </i>
      </el-row>
    </el-form>
  </MyDialog>
</template>

<script>
import dialogMixin from "@/mixins/dialogMixin";
import { DEVICE_TYPE } from "@/config/enum.js";
import { fetchDeviceQuery } from "@/api/userTeam";
import { fetchLevelList } from "@/api/useManage";
export default {
  mixins: [dialogMixin],
  props: {
    btnLoading: {
      type: Boolean,
      default: false,
    },
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      DEVICE_TYPE,
      delArr:[],
      loading: false,
      gradeList: [],
      modalFormData: {
        name: "",
        remark:"",
        profits: [
          {
            gradeLevel: "",
            recommendProfit: undefined,
            bandwidthProfit: undefined,
          },
        ],
      }, // 模态框表单数据
      modalRules: {
        name: [
          { required: true, message: "请输入团队名称", trigger: "blur" },
          { max: 20, message: "团队名称最多20个字符", trigger: "blur" },
        ],
        profits: {
          gradeLevel: [
            {
              required: false,
              message: "请选择等级",
              trigger: "blur",
            },
          ],
          recommendProfit: [
            {
              required: false,
              message: "请输入分销成本",
              trigger: "blur",
            },
          ],
          bandwidthProfit: [
            {
              required: false,
              message: "请输入流量分润比例",
              trigger: "blur",
            },
          ],
        },
      }, // 模态框表单验证规则
      BILL_TYPE:[{
        label:"月结",
        value:0
      },{
        label:"周结",
        value:1
      },{
        label:"日结",
        value:2
      }]
    };
  },
  watch: {
    id: {
      handler(val) {
        if (val) {
          this.getDetail(val);
        } else {
          this.modalFormData = {
            name: "",
            deviceTypeValue: "",
            profits: [
              {
                gradeLevel: "",
                recommendProfit: undefined,
                bandwidthProfit: undefined,
              },
            ],
          };
        }
      },
      immediate: true,
    },
  },
  mounted() {
    fetchLevelList().then((res) => {
      this.gradeList = res.data;
    });
  },
  methods: {
    handleAdd() {
      this.modalFormData.profits.push({
        gradeLevel: "",
        recommendProfit: undefined,
        bandwidthProfit: undefined,
      });
    },
    handleRemove(index) {
      if(this.modalFormData.profits[index].id){
        this.delArr.push(this.modalFormData.profits[index].id)
      }
      this.modalFormData.profits.splice(index, 1);
      console.log(this.delArr)
    },
    getDetail(id) {
      this.loading = true;
      fetchDeviceQuery({ id })
        .then((res) => {
          this.modalFormData = res.data;
          if (!this.modalFormData.profits.length) {
            this.modalFormData.profits = [
              {
                gradeLevel: "",
                recommendProfit: undefined,
                bandwidthProfit: undefined,
              },
            ];
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleConfirmClick() {
      this.$refs.baseForm.validate((valid) => {
        if (valid) {
          this.$emit("confirm", [this.modalFormData, this.delArr]);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.input-number {
  ::v-deep .el-form-item__label {
    width: 180px !important;
  }
}
.input-number-row ::v-deep .input-number:last-child .el-form-item__label {
  width: 208px !important;
}
.icon {
  margin: 6px 0 0 20px;
  font-size: 26px;
  color: #999;
  cursor: pointer;
}
</style>
